SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DeleteTipoMaquina]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[DeleteTipoMaquina]
(
	@Original_NOMBRE_TIPO_MAQUINA char(40)
)
AS
	SET NOCOUNT OFF;
DELETE FROM [TIPO_MAQUINA] WHERE (([NOMBRE_TIPO_MAQUINA] = @Original_NOMBRE_TIPO_MAQUINA))' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ValidarUsuario]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ValidarUsuario]
	-- Add the parameters for the stored procedure here
	
@RUT varchar(12), 
@PASS varchar(20)
	
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.

	if(SELECT PASS_CLIENTE FROM CLIENTE WHERE RUT_CLIENTE=@RUT )=@PASS
    -- Insert statements for procedure here
		BEGIN
			RETURN 1
		END
	ELSE
		BEGIN
		if(SELECT PASS_AGENTE FROM AGENTE WHERE RUT_AGENTE=@RUT )=@PASS
    		BEGIN
				RETURN 1
			END
				RETURN 0

		END
	
END

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AGENTE]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[AGENTE](
	[RUT_AGENTE] [char](12) NOT NULL,
	[NOMBRE_AGENTE] [text] NOT NULL,
	[UBICACION_ACTUAL_AGENTE] [text] NOT NULL,
	[TIPO_AGENTE] [int] NOT NULL,
	[ESTADO_AGENTE] [bit] NULL,
	[PASS_AGENTE] [char](20) NOT NULL,
 CONSTRAINT [PK_AGENTE] PRIMARY KEY NONCLUSTERED 
(
	[RUT_AGENTE] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ESTADO]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[ESTADO](
	[ESTADO_NOMBRE] [char](250) NOT NULL,
	[ESTADO_DESCRIPCION] [text] NULL,
 CONSTRAINT [PK_ESTADO] PRIMARY KEY NONCLUSTERED 
(
	[ESTADO_NOMBRE] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TIPO_MAQUINA]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[TIPO_MAQUINA](
	[NOMBRE_TIPO_MAQUINA] [char](60) NOT NULL,
	[DESCRIPCION_TIPO_MAQUINA] [text] NULL,
 CONSTRAINT [PK_TIPO_MAQUINA] PRIMARY KEY NONCLUSTERED 
(
	[NOMBRE_TIPO_MAQUINA] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TELEFONO_AGENTE]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[TELEFONO_AGENTE](
	[NUMERO_TELEFONO_AGENTE] [char](100) NOT NULL,
	[RUT_AGENTE] [char](12) NOT NULL,
 CONSTRAINT [PK_TELEFONO_AGENTE] PRIMARY KEY NONCLUSTERED 
(
	[NUMERO_TELEFONO_AGENTE] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[TELEFONO_AGENTE]') AND name = N'UBICABLE_POR_FK')
CREATE NONCLUSTERED INDEX [UBICABLE_POR_FK] ON [dbo].[TELEFONO_AGENTE] 
(
	[RUT_AGENTE] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CLIENTE]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[CLIENTE](
	[RUT_CLIENTE] [char](12) NOT NULL,
	[RUT_AGENTE] [char](12) NOT NULL,
	[NOMBRE_CLIENTE] [text] NOT NULL,
	[EMAIL_CLIENTE] [text] NULL,
	[DIRECCION_CLIENTE] [text] NOT NULL,
	[TIPO_CLIENTE] [int] NOT NULL,
	[PASS_CLIENTE] [char](20) NULL,
 CONSTRAINT [PK_CLIENTE] PRIMARY KEY NONCLUSTERED 
(
	[RUT_CLIENTE] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
END
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[CLIENTE]') AND name = N'TIENE_FK')
CREATE NONCLUSTERED INDEX [TIENE_FK] ON [dbo].[CLIENTE] 
(
	[RUT_AGENTE] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TRANSICION]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[TRANSICION](
	[EST_ESTADO_NOMBRE] [char](250) NOT NULL,
	[ESTADO_NOMBRE] [char](250) NOT NULL,
 CONSTRAINT [PK_TRANSICION] PRIMARY KEY CLUSTERED 
(
	[EST_ESTADO_NOMBRE] ASC,
	[ESTADO_NOMBRE] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[TRANSICION]') AND name = N'DESDE_FK')
CREATE NONCLUSTERED INDEX [DESDE_FK] ON [dbo].[TRANSICION] 
(
	[EST_ESTADO_NOMBRE] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[TRANSICION]') AND name = N'HACIA_FK')
CREATE NONCLUSTERED INDEX [HACIA_FK] ON [dbo].[TRANSICION] 
(
	[ESTADO_NOMBRE] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[HISTORIAL_DE_ESTADOS]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[HISTORIAL_DE_ESTADOS](
	[ID_MAQUINA] [numeric](18, 0) NOT NULL,
	[ESTADO_NOMBRE] [char](250) NOT NULL,
 CONSTRAINT [PK_HISTORIAL_DE_ESTADOS] PRIMARY KEY CLUSTERED 
(
	[ID_MAQUINA] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[HISTORIAL_DE_ESTADOS]') AND name = N'ESTA_REGISTRADO_FK')
CREATE NONCLUSTERED INDEX [ESTA_REGISTRADO_FK] ON [dbo].[HISTORIAL_DE_ESTADOS] 
(
	[ESTADO_NOMBRE] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DETALLE_ORDEN_DE_COMPRA]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[DETALLE_ORDEN_DE_COMPRA](
	[ID_ORDEN_DE_COMPRA] [numeric](18, 0) NOT NULL,
	[LINEA_DETALLE_ORDEN_DE_COMPRA] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
	[ID_MAQUINA] [numeric](18, 0) NOT NULL,
	[MONTO_DETALLE] [int] NOT NULL,
 CONSTRAINT [PK_DETALLE_ORDEN_DE_COMPRA] PRIMARY KEY NONCLUSTERED 
(
	[ID_ORDEN_DE_COMPRA] ASC,
	[LINEA_DETALLE_ORDEN_DE_COMPRA] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[DETALLE_ORDEN_DE_COMPRA]') AND name = N'CONTIENE_FK')
CREATE NONCLUSTERED INDEX [CONTIENE_FK] ON [dbo].[DETALLE_ORDEN_DE_COMPRA] 
(
	[ID_MAQUINA] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[DETALLE_ORDEN_DE_COMPRA]') AND name = N'POSEE_FK')
CREATE NONCLUSTERED INDEX [POSEE_FK] ON [dbo].[DETALLE_ORDEN_DE_COMPRA] 
(
	[ID_ORDEN_DE_COMPRA] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ORDEN_DE_COMPRA]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[ORDEN_DE_COMPRA](
	[ID_ORDEN_DE_COMPRA] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
	[RUT_CLIENTE] [char](12) NOT NULL,
	[FORMA_DE_PAGO_ORDEN_COMPRA] [int] NOT NULL,
	[FECHA_ORDEN_DE_COMPRA] [datetime] NOT NULL,
	[ESTADO_ORDEN_DE_COMPRA] [bit] NOT NULL,
	[MONTO] [int] NOT NULL,
 CONSTRAINT [PK_ORDEN_DE_COMPRA] PRIMARY KEY NONCLUSTERED 
(
	[ID_ORDEN_DE_COMPRA] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[ORDEN_DE_COMPRA]') AND name = N'OBTIENE_FK')
CREATE NONCLUSTERED INDEX [OBTIENE_FK] ON [dbo].[ORDEN_DE_COMPRA] 
(
	[RUT_CLIENTE] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SOLICITUD_CONTACTO]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[SOLICITUD_CONTACTO](
	[ID_SOLICITUD_CONTACTO] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
	[RUT_CLIENTE] [char](12) NOT NULL,
	[DESCRIPCION_SOLICITUD_CONTACTO] [nchar](250) NULL,
 CONSTRAINT [PK_SOLICITUD_CONTACTO] PRIMARY KEY NONCLUSTERED 
(
	[ID_SOLICITUD_CONTACTO] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[SOLICITUD_CONTACTO]') AND name = N'SOLICITA_FK')
CREATE NONCLUSTERED INDEX [SOLICITA_FK] ON [dbo].[SOLICITUD_CONTACTO] 
(
	[RUT_CLIENTE] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
GO
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'MS_Description' , N'SCHEMA',N'dbo', N'TABLE',N'SOLICITUD_CONTACTO', NULL,NULL))
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'debe ponerse al menos uno de los tres campos de registro (telefono, mail, direccion)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SOLICITUD_CONTACTO'
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TELEFONO_CLIENTE]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[TELEFONO_CLIENTE](
	[NUMERO_TELEFONO_CLIENTE] [char](100) NOT NULL,
	[RUT_CLIENTE] [char](12) NULL,
 CONSTRAINT [PK_TELEFONO_CLIENTE] PRIMARY KEY NONCLUSTERED 
(
	[NUMERO_TELEFONO_CLIENTE] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[TELEFONO_CLIENTE]') AND name = N'CONTACTA_POR_FK')
CREATE NONCLUSTERED INDEX [CONTACTA_POR_FK] ON [dbo].[TELEFONO_CLIENTE] 
(
	[RUT_CLIENTE] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MAQUINA]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[MAQUINA](
	[ID_MAQUINA] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
	[NOMBRE_TIPO_MAQUINA] [char](60) NOT NULL,
	[NOMBRE_MAQUINA] [varchar](60) NOT NULL,
	[DESCRIPCION_MAQUINA] [text] NULL,
	[IMAGEN] [nchar](60) NULL,
 CONSTRAINT [PK_MAQUINA] PRIMARY KEY NONCLUSTERED 
(
	[ID_MAQUINA] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
END
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[MAQUINA]') AND name = N'ES_DE_FK')
CREATE NONCLUSTERED INDEX [ES_DE_FK] ON [dbo].[MAQUINA] 
(
	[NOMBRE_TIPO_MAQUINA] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_TELEFONO_UBICABLE__AGENTE]') AND parent_object_id = OBJECT_ID(N'[dbo].[TELEFONO_AGENTE]'))
ALTER TABLE [dbo].[TELEFONO_AGENTE]  WITH CHECK ADD  CONSTRAINT [FK_TELEFONO_UBICABLE__AGENTE] FOREIGN KEY([RUT_AGENTE])
REFERENCES [dbo].[AGENTE] ([RUT_AGENTE])
GO
ALTER TABLE [dbo].[TELEFONO_AGENTE] CHECK CONSTRAINT [FK_TELEFONO_UBICABLE__AGENTE]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_CLIENTE_TIENE_AGENTE]') AND parent_object_id = OBJECT_ID(N'[dbo].[CLIENTE]'))
ALTER TABLE [dbo].[CLIENTE]  WITH CHECK ADD  CONSTRAINT [FK_CLIENTE_TIENE_AGENTE] FOREIGN KEY([RUT_AGENTE])
REFERENCES [dbo].[AGENTE] ([RUT_AGENTE])
GO
ALTER TABLE [dbo].[CLIENTE] CHECK CONSTRAINT [FK_CLIENTE_TIENE_AGENTE]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_TRANSICI_DESDE_ESTADO]') AND parent_object_id = OBJECT_ID(N'[dbo].[TRANSICION]'))
ALTER TABLE [dbo].[TRANSICION]  WITH CHECK ADD  CONSTRAINT [FK_TRANSICI_DESDE_ESTADO] FOREIGN KEY([EST_ESTADO_NOMBRE])
REFERENCES [dbo].[ESTADO] ([ESTADO_NOMBRE])
GO
ALTER TABLE [dbo].[TRANSICION] CHECK CONSTRAINT [FK_TRANSICI_DESDE_ESTADO]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_TRANSICI_HACIA_ESTADO]') AND parent_object_id = OBJECT_ID(N'[dbo].[TRANSICION]'))
ALTER TABLE [dbo].[TRANSICION]  WITH CHECK ADD  CONSTRAINT [FK_TRANSICI_HACIA_ESTADO] FOREIGN KEY([ESTADO_NOMBRE])
REFERENCES [dbo].[ESTADO] ([ESTADO_NOMBRE])
GO
ALTER TABLE [dbo].[TRANSICION] CHECK CONSTRAINT [FK_TRANSICI_HACIA_ESTADO]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_HISTORIA_AGREGA_MAQUINA]') AND parent_object_id = OBJECT_ID(N'[dbo].[HISTORIAL_DE_ESTADOS]'))
ALTER TABLE [dbo].[HISTORIAL_DE_ESTADOS]  WITH CHECK ADD  CONSTRAINT [FK_HISTORIA_AGREGA_MAQUINA] FOREIGN KEY([ID_MAQUINA])
REFERENCES [dbo].[MAQUINA] ([ID_MAQUINA])
GO
ALTER TABLE [dbo].[HISTORIAL_DE_ESTADOS] CHECK CONSTRAINT [FK_HISTORIA_AGREGA_MAQUINA]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_HISTORIA_ESTA_REGI_ESTADO]') AND parent_object_id = OBJECT_ID(N'[dbo].[HISTORIAL_DE_ESTADOS]'))
ALTER TABLE [dbo].[HISTORIAL_DE_ESTADOS]  WITH CHECK ADD  CONSTRAINT [FK_HISTORIA_ESTA_REGI_ESTADO] FOREIGN KEY([ESTADO_NOMBRE])
REFERENCES [dbo].[ESTADO] ([ESTADO_NOMBRE])
GO
ALTER TABLE [dbo].[HISTORIAL_DE_ESTADOS] CHECK CONSTRAINT [FK_HISTORIA_ESTA_REGI_ESTADO]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_DETALLE__CONTIENE_MAQUINA]') AND parent_object_id = OBJECT_ID(N'[dbo].[DETALLE_ORDEN_DE_COMPRA]'))
ALTER TABLE [dbo].[DETALLE_ORDEN_DE_COMPRA]  WITH CHECK ADD  CONSTRAINT [FK_DETALLE__CONTIENE_MAQUINA] FOREIGN KEY([ID_MAQUINA])
REFERENCES [dbo].[MAQUINA] ([ID_MAQUINA])
GO
ALTER TABLE [dbo].[DETALLE_ORDEN_DE_COMPRA] CHECK CONSTRAINT [FK_DETALLE__CONTIENE_MAQUINA]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_DETALLE__POSEE_ORDEN_DE]') AND parent_object_id = OBJECT_ID(N'[dbo].[DETALLE_ORDEN_DE_COMPRA]'))
ALTER TABLE [dbo].[DETALLE_ORDEN_DE_COMPRA]  WITH CHECK ADD  CONSTRAINT [FK_DETALLE__POSEE_ORDEN_DE] FOREIGN KEY([ID_ORDEN_DE_COMPRA])
REFERENCES [dbo].[ORDEN_DE_COMPRA] ([ID_ORDEN_DE_COMPRA])
GO
ALTER TABLE [dbo].[DETALLE_ORDEN_DE_COMPRA] CHECK CONSTRAINT [FK_DETALLE__POSEE_ORDEN_DE]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_ORDEN_DE_OBTIENE_CLIENTE]') AND parent_object_id = OBJECT_ID(N'[dbo].[ORDEN_DE_COMPRA]'))
ALTER TABLE [dbo].[ORDEN_DE_COMPRA]  WITH CHECK ADD  CONSTRAINT [FK_ORDEN_DE_OBTIENE_CLIENTE] FOREIGN KEY([RUT_CLIENTE])
REFERENCES [dbo].[CLIENTE] ([RUT_CLIENTE])
GO
ALTER TABLE [dbo].[ORDEN_DE_COMPRA] CHECK CONSTRAINT [FK_ORDEN_DE_OBTIENE_CLIENTE]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_SOLICITU_SOLICITA_CLIENTE]') AND parent_object_id = OBJECT_ID(N'[dbo].[SOLICITUD_CONTACTO]'))
ALTER TABLE [dbo].[SOLICITUD_CONTACTO]  WITH CHECK ADD  CONSTRAINT [FK_SOLICITU_SOLICITA_CLIENTE] FOREIGN KEY([RUT_CLIENTE])
REFERENCES [dbo].[CLIENTE] ([RUT_CLIENTE])
GO
ALTER TABLE [dbo].[SOLICITUD_CONTACTO] CHECK CONSTRAINT [FK_SOLICITU_SOLICITA_CLIENTE]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_TELEFONO_CONTACTA__CLIENTE]') AND parent_object_id = OBJECT_ID(N'[dbo].[TELEFONO_CLIENTE]'))
ALTER TABLE [dbo].[TELEFONO_CLIENTE]  WITH CHECK ADD  CONSTRAINT [FK_TELEFONO_CONTACTA__CLIENTE] FOREIGN KEY([RUT_CLIENTE])
REFERENCES [dbo].[CLIENTE] ([RUT_CLIENTE])
GO
ALTER TABLE [dbo].[TELEFONO_CLIENTE] CHECK CONSTRAINT [FK_TELEFONO_CONTACTA__CLIENTE]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_MAQUINA_ES_DE_TIPO_MAQ]') AND parent_object_id = OBJECT_ID(N'[dbo].[MAQUINA]'))
ALTER TABLE [dbo].[MAQUINA]  WITH CHECK ADD  CONSTRAINT [FK_MAQUINA_ES_DE_TIPO_MAQ] FOREIGN KEY([NOMBRE_TIPO_MAQUINA])
REFERENCES [dbo].[TIPO_MAQUINA] ([NOMBRE_TIPO_MAQUINA])
GO
ALTER TABLE [dbo].[MAQUINA] CHECK CONSTRAINT [FK_MAQUINA_ES_DE_TIPO_MAQ]
